package controller

import (
	"encoding/json"
	"fmt"
	"hsy/server/framework"
	"hsy/server/service"
	"io/ioutil"
	"log"
	"net/http"
	"reflect"
)

//CategoryController ...
type CategoryController struct {
}

//Router ...
func (c *CategoryController) Router(route *framework.RouterHandler) {

	route.Router("/category/add", c.addCategory)
}

//CategoryService ...
var categoryService = new(service.CategoryService)

func (c *CategoryController) addCategory(w http.ResponseWriter, r *http.Request) {

	// if r.Method != "POST" {
	// 	framework.ResultJSON(w, framework.NewResult(405, "请求方法不正确", nil))
	// }

	formData := make(map[string]interface{})
	decoder := json.NewDecoder(r.Body)
	decoder.UseNumber()
	// 调用json包的解析，解析请求body
	decoder.Decode(&formData)
	for key, value := range formData {
		log.Println("key:", key, " => value :", value, "valueType:", reflect.TypeOf(value).String())
	}

	body, _ := ioutil.ReadAll(r.Body)
	fmt.Println(string(body))

	//fmt.Println(r.PostFormValue("categoryName"))
	var data framework.Result

	// id := categoryService.AddCategory(r.Form["categoryName"][0],
	// 	r.Form["categoryDesc"][0],
	// 	1, 0)

	id := 1

	if id > 0 {
		data = framework.NewResult(200, "success", nil)
	} else {
		data = framework.NewResult(201, "添加分类失败", nil)
	}
	framework.ResultJSON(w, data)

}
